package com.hejielife.singlethreadedexecution1;

/**
 * 2019年8月2日 上午10:13:39
 * 
 * @author hejielife
 * @despribe 模拟一个门
 */
public class Gate {
	private int counter = 0;
	private String name = "Nobody";
	private String address = "Nowhere";

	// 保护counter、name、address变量的安全。不安全的方法同步保护
	public synchronized void pass(String name, String address) {
		this.counter++;
		this.name = name;
		this.address = address;
		System.out.println("***** PASS ***** " + toString());
		check();
	}

	public synchronized String toString() {
		return "No." + counter + ": " + name + ", " + address;
	}

	private void check() {
		if (name.charAt(0) != address.charAt(0)) {
			System.out.println("***** BROKEN ***** " + toString());
		}
	}
}
